package com.example.myapplication.ui;


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.example.myapplication.R;
import com.example.myapplication.tools.SocketTools;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketDoor extends Activity {

    String TAG = "-=-=>>";
    String SN = "BYXI222360036";
    SocketTools socketTools;
    boolean isRun = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_socket_door);
        isRun = true;
        initView();
    }

    @Override
    protected void onDestroy() {
        isRun = false;
        super.onDestroy();
    }

    private void initView(){
//        socketTools = new SocketTools("192.168.215.31", 20000, new SocketTools.ReadMessage() {
//            @Override
//            public void message(String msg) {
//                Log.e(TAG, "message: " + msg);
//            }
//
//            @Override
//            public void error(String errorInfo) {
//                Log.e(TAG, "error: " + errorInfo);
//            }
//        });
        new Thread(){
            @Override
            public void run() {
//                socketTools.createSocket("");
                startSocketService(5566);
            }
        }.start();

        new Thread(){
            @Override
            public void run() {
//                socketTools.createSocket("");
                startSocketService(5567);
            }
        }.start();
    }

    public void onClickVoid(View v){
        switch (v.getId()){
            case R.id.button1:
                String temp = "{\"funcId\": \"device.upload.heartbeat\", \"payload\": {\"params\": {\"sn\": \"" + SN + "\"}}} ";
                socketTools.send(temp);
                break;
        }
    }

    private void startSocketService(int port){
        try {
            ServerSocket server = new ServerSocket(port);
            while(isRun){
                // 等待连接
                Socket accept = server.accept();
                // 读取数据
                String coding = "UTF8";
                InputStream inputStream = accept.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        inputStream,coding));
                String ss = "";
                ss = reader.readLine();
                StringBuilder sb = new StringBuilder();
                while (ss != null && ss.length() > 0){
                    sb.append(ss);
                    ss = reader.readLine();
                }
                Log.e(TAG, port+"服务器：: " + sb.toString());
                String temp = "{\"'code\":0,\"message\": \"\" ," +
                        "\"payload\":{\"params\": {\"command\":0,\"secret\":\"abc\",\"ip\":\"192.168.3.132\",\"port\":5566}}}";

                OutputStream outputStream = accept.getOutputStream();
                outputStream.write((temp + "\r\n").getBytes(coding));
//                outputStream.write("你好，我是服务器！\r\n".getBytes());
                outputStream.flush();
                outputStream.close();
                inputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }




}